package handler

import (
	"github.com/gin-gonic/gin"
	"path/filepath"
	"schools/api-getaway/pkg"
)

func Upload(r *gin.Context) {
	file, err := r.FormFile("image")
	if err != nil {
		return
	}

	upload, err := pkg.Upload(file)
	if err != nil {
		r.JSON(400, &Res{
			Code:    400,
			Message: "获取文件失败",
			Data:    nil,
		})
		return
	}

	if file.Size >= 2*1024*1024 {
		r.JSON(400, &Res{
			Code:    400,
			Message: "图片大小不正确",
			Data:    nil,
		})
		return
	}

	ext := filepath.Ext(file.Filename)
	if ext != ".png" {
		r.JSON(400, &Res{
			Code:    400,
			Message: "图片格式不正确",
			Data:    nil,
		})
		return
	}

	r.JSON(200, &Res{
		Code:    200,
		Message: "图片上传成功",
		Data:    upload,
	})
}
